package braindamagestudio.com.app.fight;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.IEntity;
import org.anddev.andengine.entity.modifier.DelayModifier;
import org.anddev.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.anddev.andengine.entity.modifier.LoopEntityModifier;
import org.anddev.andengine.entity.modifier.MoveModifier;
import org.anddev.andengine.entity.modifier.ParallelEntityModifier;
import org.anddev.andengine.entity.modifier.RotationModifier;
import org.anddev.andengine.entity.modifier.SequenceEntityModifier;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTexture;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;
import org.anddev.andengine.util.modifier.IModifier;
import org.anddev.andengine.util.modifier.ease.EaseElasticInOut;

import ResourceManager.ResourceManager;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
import appfight.icon.manager.IconManager;
import appfight.importApp.ImportAppMenu;
import appfight.scene.manager.SceneManager;

public class AppFightActivity extends BaseGameActivity {
    /** Called when the activity is first created. */
    private static final int CAMERA_WIDTH = 480;
	private static final int CAMERA_HEIGHT = 800;
	
	
	//just for test
	private ResourceManager _resourceManager;
	private SceneManager _sceneManager;

	// ===========================================================
	// Fields
	// ===========================================================

	private Camera mCamera;
	// ===========================================================
	// Constructors
	// ===========================================================

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	@Override
	public Engine onLoadEngine() {
		Log.d("gameloop", "onLoadEngine");
		
		this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		return new Engine(new EngineOptions(true, ScreenOrientation.PORTRAIT, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));
}

	private BitmapTexture BackGroundTexture;
	private TextureRegion BackGroundRegion;
	private BitmapTexture TitleTexture;
	private TextureRegion TitleRegion;
	private BitmapTexture ConstantCTexture;
	private TextureRegion ConstantCRegion;
	
	
	private BitmapTexture KnifeTexture;
	private TextureRegion Knife1Region;
	private TextureRegion Knife2Region;
	
	private BitmapTexture TextTexture;
	private TextureRegion StartRegion;
	private TextureRegion ImportRegion;
	
	@Override
	public void onLoadResources() {
		_resourceManager = new ResourceManager(this);
		
		Log.d("gameloop", "onLoadResources");
		
		this.BackGroundTexture = new BitmapTexture(512, 1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.BackGroundRegion = BitmapTextureRegionFactory.createFromAsset(this.BackGroundTexture, this, "StartActivityBackGround.png", 0, 0);
		this.mEngine.getTextureManager().loadTexture(this.BackGroundTexture);
		
		this.TitleTexture = new BitmapTexture(512, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.TitleRegion = BitmapTextureRegionFactory.createFromAsset(this.TitleTexture, this, "StartActivityTitle.png", 0, 0);
		this.mEngine.getTextureManager().loadTexture(this.TitleTexture);
		
		this.ConstantCTexture = new BitmapTexture(128, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.ConstantCRegion = BitmapTextureRegionFactory.createFromAsset(this.ConstantCTexture, this, "ConstantCIcon.png", 0, 0);
		this.mEngine.getTextureManager().loadTexture(this.ConstantCTexture);
		
		this.KnifeTexture = new BitmapTexture(64, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.Knife1Region = BitmapTextureRegionFactory.createFromAsset(this.KnifeTexture, this, "Knife.png", 0, 0);
		this.Knife2Region = BitmapTextureRegionFactory.createFromAsset(this.KnifeTexture, this, "Knife2.png", 32, 0);
		this.mEngine.getTextureManager().loadTexture(this.KnifeTexture);
		
		this.TextTexture = new BitmapTexture(256, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.StartRegion = BitmapTextureRegionFactory.createFromAsset(this.TextTexture, this, "Start.png", 0, 0);
		this.ImportRegion = BitmapTextureRegionFactory.createFromAsset(this.TextTexture, this, "Import.png", 0, 32);
		this.mEngine.getTextureManager().loadTexture(this.TextTexture);
		
	}

	//Sprite ConstantC2;
	
	@Override
	public Scene onLoadScene() {
		Log.d("gameloop", "onLoadScene");			
		
		this.mEngine.registerUpdateHandler(new FPSLogger());

		final Scene scene = new Scene();
		scene.setBackground(new ColorBackground(0.09804f, 0.6274f, 0.8784f));

		_sceneManager = new SceneManager(scene);
		
		/* Calculate the coordinates for the face, so its centered on the camera. */
		final Sprite BackGround = new Sprite(0, 0, this.BackGroundRegion);
		final Sprite Title = new Sprite(0,50,this.TitleRegion);
		final Sprite ConstantC = new Sprite(600,380,this.ConstantCRegion);
		
		
		
		float scale = 3;		
		final Sprite Knife1 = new Sprite(135,33,this.Knife1Region);		
		final Sprite Knife2 = new Sprite(-40,60,this.Knife2Region);
		Knife2.setScale(scale);
		Knife1.setScale(scale);
		
		final Sprite Start = new Sprite(600,580,this.StartRegion){
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) 
			{
				onStartButtonTouch(pSceneTouchEvent,pTouchAreaLocalX,pTouchAreaLocalY);
				return false;
			}
		};
		
		
		final Sprite Import = new Sprite(600,650,this.ImportRegion){
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) 
			{
				onImportButtonTouch(pSceneTouchEvent,pTouchAreaLocalX,pTouchAreaLocalY);
				return false;
			}
		};
		
		
	//	UnloadResourcesExample.this.runOnUiThread(new Runnable() {
	//		@Override
	//		public void run() {
	//			scene.detachChild(thisRef);
	//		}
	//	});
		
		
		scene.attachChild(BackGround);
		scene.attachChild(Title);
		scene.attachChild(ConstantC);
		
		ConstantC.attachChild(Knife1);
		ConstantC.attachChild(Knife2);
		
		scene.attachChild(Start);
		scene.attachChild(Import);
		ConstantC.registerEntityModifier(
				
				new SequenceEntityModifier(		
						new IEntityModifierListener()
				{

					@Override
					public void onModifierStarted(IModifier<IEntity> pModifier,
							IEntity pItem) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void onModifierFinished(
							IModifier<IEntity> pModifier, IEntity pItem) {
						// TODO Auto-generated method stub
						ConstantC.registerEntityModifier(
								
								new LoopEntityModifier(
										
										new SequenceEntityModifier(
												
										new ParallelEntityModifier(
												new RotationModifier(1, -5, 5)
										),
										new ParallelEntityModifier(
												new RotationModifier(1, 5, -5)
											
										)
										)
										)
								);	

					}					
				} 
				,				
				new DelayModifier(0.5f),
				
				new ParallelEntityModifier(
				new MoveModifier(2,600,178, 380, 400, EaseElasticInOut.getInstance())	,
				new RotationModifier(2, 60, -5)
				)
						)	
				);
		
		Start.registerEntityModifier(
				
				new SequenceEntityModifier(						
				new DelayModifier(1f),
				new MoveModifier(2,600,155, 580, 600, EaseElasticInOut.getInstance())				
						)	
				);
		
		Import.registerEntityModifier(
				
				new SequenceEntityModifier(						
				new DelayModifier(1.2f),
				new MoveModifier(2,600,155, 650, 670, EaseElasticInOut.getInstance())				
						)	
				);
		scene.registerTouchArea(Start);
		scene.registerTouchArea(Import);
		
		return scene;
	}
	
	private void onStartButtonTouch(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) 
	{		
		if(pSceneTouchEvent.isActionDown())
		{
			IconManager m = new IconManager(this);
			
			if(m.getAllIconsPath().Names.size()<10)
				Toast.makeText(this, "Please import at least 10 apps to start our story : )", Toast.LENGTH_SHORT).show();
			else
				Launcher.StartActivity(this,MainGameActivity.class);
		}
	}	
	
	private void onImportButtonTouch(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) 
	{		
		if(pSceneTouchEvent.isActionDown())
		{
			this.startActivity(new Intent(this,ImportAppMenu.class));
		}
	}	

	@Override
	public void onLoadComplete() {
		Log.d("gameloop", "onLoadComplete");
	}
}